package com.youkeyi.ddy.cloud.application.system.pay.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.pay.SystemPayTypeCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.pay.dto.SystemPayTypeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.pay.dto.SystemPayTypeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.pay.param.SystemPayTypeCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.pay.param.SystemPayTypeDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.pay.param.SystemPayTypeListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.pay.param.SystemPayTypeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.PublicPayTypeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.dto.PublicPayTypeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.param.PublicPayTypeCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.param.PublicPayTypeDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.param.PublicPayTypeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.param.PublicPayTypeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * SystemPayTypeController
 *
 * @Author liuxiawang
 * @Date 2024/4/17 3:11 PM
 * @Location shenzhen.china
 */
@Service
public class SystemPayTypeController implements SystemPayTypeCloudService {
    @Autowired
    private PublicPayTypeCloudService publicPayTypeCloudService;

    @Override
    public SystemPayTypeListCloudDto list(SystemPayTypeListCloudParam param) {
        SystemPayTypeListCloudDto dto = new SystemPayTypeListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicPayTypeQueryCloudParam annexTypeQueryCloudParam = new PublicPayTypeQueryCloudParam();
        annexTypeQueryCloudParam.setPayTypePrimaryKeys(param.getPayTypePrimaryKeys());
        annexTypeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        annexTypeQueryCloudParam.setCodes(param.getCodes());
        annexTypeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        annexTypeQueryCloudParam.setPageSize(param.getPageSize());
        annexTypeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicPayTypeListCloudDto annexTypeDtos = publicPayTypeCloudService.query(annexTypeQueryCloudParam);
        if (null != annexTypeDtos && CollectionUtils.isNotEmpty(annexTypeDtos.getRows())) {
            List<SystemPayTypeListItemCloudDto> items = new ArrayList<>();
            annexTypeDtos.getRows().forEach(annexType -> {
                SystemPayTypeListItemCloudDto item = new SystemPayTypeListItemCloudDto();
                item.setPayTypePrimaryKey(annexType.getPayTypePrimaryKey());
                item.setCorpKey(annexType.getCorpKey());
                item.setCode(annexType.getCode());
                item.setName(annexType.getName());
                item.setSort(annexType.getSort());
                item.setIsDeleted(annexType.getIsDeleted());
                item.setIsDeletedName(annexType.getIsDeletedName());
                item.setCreateUserKey(annexType.getCreateUserKey());
                item.setCreateTime(annexType.getCreateTime());
                item.setUpdateUserKey(annexType.getUpdateUserKey());
                item.setUpdateTime(annexType.getUpdateTime());
                item.setCreateUserName(annexType.getCreateUserName());
                item.setUpdateUserName(annexType.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(annexTypeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(SystemPayTypeCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicPayTypeCreateCloudParam annexTypeCreateCloudParam = new PublicPayTypeCreateCloudParam();
        annexTypeCreateCloudParam.setCorpKey(corpKey);
        annexTypeCreateCloudParam.setSort(param.getSort());
        annexTypeCreateCloudParam.setName(param.getName());
        annexTypeCreateCloudParam.setCode(param.getCode());
        annexTypeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        annexTypeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicPayTypeCloudService.create(annexTypeCreateCloudParam);
    }

    @Override
    public ResponseContent update(SystemPayTypeUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getPayTypePrimaryKey())
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicPayTypeUpdateCloudParam annexTypeUpdateCloudParam = new PublicPayTypeUpdateCloudParam();
        annexTypeUpdateCloudParam.setPayTypePrimaryKey(param.getPayTypePrimaryKey());
        annexTypeUpdateCloudParam.setName(param.getName());
        annexTypeUpdateCloudParam.setSort(param.getSort());
        annexTypeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        annexTypeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicPayTypeCloudService.update(annexTypeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(SystemPayTypeDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getPayTypePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicPayTypeDeleteCloudParam annexTypeDeleteCloudParam = new PublicPayTypeDeleteCloudParam();
        annexTypeDeleteCloudParam.setPayTypePrimaryKeys(param.getPayTypePrimaryKeys());
        annexTypeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        annexTypeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicPayTypeCloudService.delete(annexTypeDeleteCloudParam);
    }
}
